module.exports = ['$scope','$http', function ($scope,$http) {
    var _API = require('_API')($http);
    var shopid;
    //获取所有门店信息
    (function shop_all() {
        _API.shop.all().success(function (res) {
            $scope.shop_all_data = res;
        })
    })();

    //每个门店的点击事件
    $scope.shop_all_list = function (list, data) {
        for(var i in data){
            data[i].isActive = false;
        };
        list.isActive = true;
        shopid = list.id;
        _API.shop_user.users_of_shop(list.id).success(function (res) {
            $scope.shop_users_list = res;
        });
    };

    //获取没有门店的用户信息
    function users_shop_null() {
        _API.shop_user.users_without_shop().success(function (res) {
            $scope.users_null_shop = res;
        });
    }
    users_shop_null();

    //add
    $scope.add_shop = function (uid) {
        if(!shopid){
            showToast('请选择门店','info');
            return;
        }
        _API.shop_user.add_user_to_shop(uid, shopid).success(function(){
            showToast('添加用户成功','ok');
            users_shop_null();
            _API.shop_user.users_of_shop(shopid).success(function (res) {
                $scope.shop_users_list = res;
            });
        });
    };

    //del
    $scope.del_user_shop = function (uid) {
        console.log(uid);
        _API.shop_user.remove_user_from_shop(uid).success(function(){
            showToast('删除用户成功','ok');
            users_shop_null();
            _API.shop_user.users_of_shop(shopid).success(function (res) {
                $scope.shop_users_list = res;
            });
        });
    }
}];